package com.ccx.library.module.parallel.controller;

import com.ccx.library.module.parallel.mapper.RoleMenuMapper;
import com.ccx.library.module.parallel.service.RoleMenuService;
import com.ccx.library.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private RoleMenuService roleMenuService;

    @PostMapping("/bindMenu/{roleId}/{menuId}")
    public R bindMenu(@PathVariable("roleId")String roleId,
                      @PathVariable("menuId")String menuId){
        return transactionTemplate.execute((Status) -> {
           try{
               roleMenuService.save(roleId, menuId);
           }catch (Exception e){
               Status.setRollbackOnly();
               return R.FAIL("绑定失败");
           }
           return R.SUCCESS("绑定成功");
        });
    }
}
